package controller

import (
	"dc-news/common/global/consts"
	myjwt "dc-news/common/middleware/jwt"
	"dc-news/common/resp"
	"dc-news/service"
	"dc-news/vo"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"net/http"
)

// AddCommentHandler 添加评论
func AddCommentHandler(c *gin.Context) {
	claims := c.MustGet("claims").(*myjwt.CustomClaims)
	req := &vo.AddCommentReq{}
	err := c.Bind(req)
	if err != nil {
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ParameterErrorCode, "参数错误"))
		return
	}
	service.AddCommentService(c, cast.ToInt(claims.Id), req)
}

// GetCommentHandler 获取评论
func GetCommentHandler(c *gin.Context) {
	idStr, ok := c.Params.Get("id")
	id := cast.ToInt(idStr)
	if !ok || id == 0 {
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ParameterErrorCode, "参数错误"))
		return
	}
	service.GetCommentService(c, id)
}

// GetNewsCommentsHandler 获取新闻评论
func GetNewsCommentsHandler(c *gin.Context) {
	idStr, ok := c.Params.Get("id")
	id := cast.ToInt(idStr)
	if !ok || id == 0 {
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ParameterErrorCode, "参数错误"))
		return
	}
	service.GetNewsCommentService(c, id)
}

// GetCommentCommentsHandler 获取新闻评论
func GetCommentCommentsHandler(c *gin.Context) {
	idStr, ok := c.Params.Get("id")
	id := cast.ToInt(idStr)
	if !ok || id == 0 {
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ParameterErrorCode, "参数错误"))
		return
	}
	service.GetCommentCommentService(c, id)
}

// LikesCommentHandler 点赞/踩评论
func LikesCommentHandler(c *gin.Context) {
	idStr, ok := c.Params.Get("id")
	id := cast.ToInt(idStr)
	if !ok || id == 0 {
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ParameterErrorCode, "参数错误"))
		return
	}
	req := &vo.LikeCommentReq{}
	err := c.Bind(&req)
	if err != nil {
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ParameterErrorCode, "参数错误"))
		return
	}
	if req.Likes != 1 && req.Likes != 2 {
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ParameterErrorCode, "参数错误"))
		return
	}
	claims := c.MustGet("claims").(*myjwt.CustomClaims)
	service.LikesCommentService(c, id, cast.ToInt(claims.Id), req)
}

// CancelLikesCommentHandler 取评论消赞/踩
func CancelLikesCommentHandler(c *gin.Context) {
	idStr, ok := c.Params.Get("id")
	id := cast.ToInt(idStr)
	if !ok || id == 0 {
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ParameterErrorCode, "参数错误"))
		return
	}
	claims := c.MustGet("claims").(*myjwt.CustomClaims)
	service.CancelLikesCommentService(c, id, cast.ToInt(claims.Id))
}

// GetUserCommentsHandler 获取用户评论列表
func GetUserCommentsHandler(c *gin.Context) {
	claims := c.MustGet("claims").(*myjwt.CustomClaims)
	service.GetUserCommentsService(c, cast.ToInt(claims.Id))
}

// GetUserLikesCommentsHandler 获取用户评论列表
func GetUserLikesCommentsHandler(c *gin.Context) {
	claims := c.MustGet("claims").(*myjwt.CustomClaims)
	service.GetUserLikeCommentsListService(c, cast.ToInt(claims.Id))
}
